<?php
namespace App\Services;

use OSS\OssClient;
use OSS\Core\OssException;
use App\Models\Linkslists;
use Exception,Log;

class HtmlossService
{
    public static function aliossLists()
    {
        // 获取OSS服务器列表
        $ossLists = [
            // 主域名
            ["accessKeyId"=>"ldsHmLFDIu2dR1TA","accessKeySecret"=>"YW5UlfrCo0ZUdRJnUo2yf0uvgaEsmp","endpoint"=>"https://oss-cn-beijing.aliyuncs.com","bucket"=>"wailian-main"],
            // 微信/百度/微博
            ["accessKeyId"=>"ldsHmLFDIu2dR1TA","accessKeySecret"=>"YW5UlfrCo0ZUdRJnUo2yf0uvgaEsmp","endpoint"=>"https://oss-cn-beijing.aliyuncs.com","bucket"=>"wailian-other"],
            // 抖音
            ["accessKeyId"=>"ldsHmLFDIu2dR1TA","accessKeySecret"=>"YW5UlfrCo0ZUdRJnUo2yf0uvgaEsmp","endpoint"=>"https://oss-cn-beijing.aliyuncs.com","bucket"=>"wailian-douyin"],
            // 香港/全球/群发
            ["accessKeyId"=>"ldsHmLFDIu2dR1TA","accessKeySecret"=>"YW5UlfrCo0ZUdRJnUo2yf0uvgaEsmp","endpoint"=>"https://oss-cn-hongkong.aliyuncs.com","bucket"=>"wailian-global"],
        ];
        return $ossLists;
    }

    // 上传并获取代码
    public static function uploadHtml($links_id,$created_at)
    {
        try {
            $day = date("Ymd",strtotime($created_at));
            $html = file_get_contents("https://bab.moreqifu.cn/l/".$links_id);
            file_put_contents(public_path("pages/".$links_id. '.html'), $html, true);
            $pic = "pages/".$day."/".$links_id. '.html';
            $oldoss = "l/".$links_id;

            $aliossLists = self::aliossLists();
            foreach ($aliossLists as $aliossList) {
                try {
                    $ossClient = new OssClient($aliossList["accessKeyId"], $aliossList["accessKeySecret"], $aliossList["endpoint"]);
                    $ossClient->uploadFile($aliossList["bucket"], $pic, public_path('pages/'.$links_id.'.html'));
                    // 老的页面路径
                    $oldOssClient = new OssClient($aliossList["accessKeyId"], $aliossList["accessKeySecret"], $aliossList["endpoint"]);
                    $oldOssClient->uploadFile($aliossList["bucket"], $oldoss, public_path('pages/'.$links_id.'.html'));
                } catch (Exception $e) {
                    continue;
                }
            }
        } catch (Exception $e) {
            Log::info("uploadHtml-------");
            Log::info($e);
            Log::info("uploadHtml-------");
        }
        try {
            unlink(public_path('pages/' . $links_id . '.html'));
        } catch (Exception $e) {
            
        }
    }

    // 上传并获取代码
    public static function uploadHtmlWfx($links_id,$link_type)
    {
        $html = file_get_contents("https://bab.moreqifu.cn/douyin_card?link_type=".$link_type."&id=".$links_id);
        file_put_contents(public_path("pages/".$links_id. '.html'), $html, true);
        $pic = "pages/".$links_id.'.html';

        $aliossLists = [
            ["accessKeyId"=>"LTAIMhniFS5j44jD","accessKeySecret"=>"mIMZwFkB8Z2o8ER8CxLGPLf9hGYpwV","endpoint"=>"https://oss-cn-shanghai.aliyuncs.com","bucket"=>"earnp"],
        ];
        foreach ($aliossLists as $aliossList) {
            try {
                $ossClient = new OssClient($aliossList["accessKeyId"], $aliossList["accessKeySecret"], $aliossList["endpoint"]);
                $ossClient->uploadFile($aliossList["bucket"],$pic, public_path('pages/'.$links_id.'.html'));
            } catch (Exception $e) {
                continue;
            }
        }
        try {
            unlink(public_path('pages/'.$links_id.'.html'));
        } catch (Exception $e) {
            
        }
    }

    public static function aliossHtmlDelete($links_id,$created_at)
    {
        if (empty($created_at)) {
            $links = Linkslists::getLinks($links_id);
            $day = date("Ymd",strtotime($links->created_at));
        } else {
            $day = date("Ymd",strtotime($created_at));
        }
        $pic = "pages/".$day."/".$links_id. '.html';
        $oldoss = "l/".$links_id;

        $aliossLists = self::aliossLists();
        foreach ($aliossLists as $aliossList) {
            try {
                $ossClient = new OssClient($aliossList["accessKeyId"], $aliossList["accessKeySecret"], $aliossList["endpoint"]);
                $ossClient->deleteObject($aliossList["bucket"], $pic);
                // 删除老的OSS
                $oldOssClient = new OssClient($aliossList["accessKeyId"], $aliossList["accessKeySecret"], $aliossList["endpoint"]);
                $oldOssClient->deleteObject($aliossList["bucket"], $oldoss);
            } catch (Exception $e) {
                continue;
            }
        }
    }

    // 上传并获取代码
    public static function uploadKefuHtml($kfid)
    {
        $html = file_get_contents("https://bab.moreqifu.cn/wxkf/".$kfid);
        $pathurl = public_path("pages/wxkf/".$kfid.".html");
        file_put_contents($pathurl, $html, true);
        $pic = "kf/".$kfid;
        $aliossLists = [
            ["accessKeyId"=>"ldsHmLFDIu2dR1TA","accessKeySecret"=>"YW5UlfrCo0ZUdRJnUo2yf0uvgaEsmp","endpoint"=>"https://oss-cn-beijing.aliyuncs.com","bucket"=>"wailian-wxkf"]
        ];
        foreach ($aliossLists as $aliossList) {
            try {
                $ossClient = new OssClient($aliossList["accessKeyId"], $aliossList["accessKeySecret"], $aliossList["endpoint"]);
                $ossClient->uploadFile($aliossList["bucket"], $pic,$pathurl);
            } catch (Exception $e) {
                continue;
            }
        }
        try {
            unlink($pathurl);
        } catch (Exception $e) {
            
        }
    }

    public static function aliossKefuHtmlDelete($kfid)
    {
        $pic = "kf/".$kfid;
        $aliossLists = [
            ["accessKeyId"=>"ldsHmLFDIu2dR1TA","accessKeySecret"=>"YW5UlfrCo0ZUdRJnUo2yf0uvgaEsmp","endpoint"=>"https://oss-cn-beijing.aliyuncs.com","bucket"=>"wailian-wxkf"]
        ];
        foreach ($aliossLists as $aliossList) {
            try {
                $ossClient = new OssClient($aliossList["accessKeyId"], $aliossList["accessKeySecret"], $aliossList["endpoint"]);
                $ossClient->deleteObject($aliossList["bucket"],$pic);
            } catch (Exception $e) {
                continue;
            }
        }
    }
}
